from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import create_openai_tools_agent,AgentExecutor,create_openai_functions_agent
from langchain_core.tools import tool
import random


api_key = "sk-6S0PtpNia71gjcfwSsDPsJ9mGqsVPr2XRQzAx1dHbJS7RW4t"
api_base="https://chatapi.littlewheat.com/v1"

llm = ChatOpenAI(model="gpt-3.5-turbo",api_key=api_key ,base_url=api_base)

prompt = hub.pull("hwchase17/openai-tools-agent")
print(prompt.messages)
print(prompt.pretty_print())
"""
================================ System Message ================================

You are a helpful assistant

============================= Messages Placeholder =============================

{chat_history}

================================ Human Message =================================

{input}

============================= Messages Placeholder =============================

{agent_scratchpad}
None
"""
print("===========================================================")
function_prompt = hub.pull("hwchase17/openai-functions-agent")
print(function_prompt.pretty_print())
"""
================================ System Message ================================

You are a helpful assistant

============================= Messages Placeholder =============================

{chat_history}

================================ Human Message =================================

{input}

============================= Messages Placeholder =============================

{agent_scratchpad}
"""

@tool
def getWeather(loc):
    """
        查询即时天气函数
        :param loc: 必要参数，字符串类型，用于表示查询天气的具体城市名称，\
        注意，中国的城市需要用对应城市的英文名称代替，例如如果需要查询上海市天气，则loc参数需要输入'ShangHai'；
        :return：OpenWeather API查询即时天气的结果，具体URL请求地址为：https://api.openweathermap.org/data/2.5/weather\
        返回结果对象类型为解析之后的JSON格式对象，并用字符串形式进行表示，其中包含了全部重要的天气信息
    """
    temp=random.randint(20,35)
    data = loc+"的当前气温是"+str(temp)
    return data

weather_function_list = [getWeather]

agent = create_openai_tools_agent(llm=llm,tools=weather_function_list,prompt=prompt)

agentExecutor = AgentExecutor(agent=agent,tools=weather_function_list)

#response=agentExecutor.invoke({"input": "今天的北京和上海的天气，现在相差多少度？"})
#print(response)